<template>
  <a-modal
    destroyOnClose
    title="选择商品"
    :zIndex="zIndex"
    v-model="value"
    :width="900"
    @cancel="onCancel()"
    :footer="null"
    :closable="false"
    :maskClosable="false"
    :bodyStyle="{ maxHeight: '73vh', overflow: 'scroll' }"
  >
    <a-card class="modal-card-body">
      <a-row align="middle" style="margin-bottom: 10px">
        <a-space class="search-item">
          商城：
          <a-select
            v-model:value="queryParam.store_id"
            show-search
            placeholder="搜索店铺"
            style="width: 200px"
            :default-active-first-option="false"
            :show-arrow="false"
            :filter-option="false"
            :not-found-content="null"
            @search="handleSearch"
            allowClear
          >
            <a-select-option v-for="(item, index) in storeList" :key="index" :value="item.id">
              {{ item.name }}</a-select-option
            >
          </a-select>
        </a-space>
        <a-space class="search-item"
          >商品名称：
          <a-input v-model="queryParam.name" placeholder="请填写搜索商品名称" />
        </a-space>
        <a-space class="search-item">
          <a-button @click="refresh" type="primary"> 搜索 </a-button>
        </a-space>
      </a-row>
      <a-row v-if="selectGoodsList.length > 0" type="flex" align="middle" class="img-list">
        <img
          class="selectImg"
          v-for="(goods1, index) of selectGoodsList"
          style="width: 30px; height: 30px; margin-right: 10px"
          :key="index"
          @click="delSelect(index)"
          :src="goods1.cover_pic"
          alt=""
        />
      </a-row>
      <selfTable ref="table" size="default" row-key="id" :columns="columns" :showPagination="true" :data="loadData">
        <template slot="name" slot-scope="name, goods">
          <a-row>
            <a-space>
              <img style="width: 40px; height: 40px" :src="goods.cover_pic" alt="" />
              ID{{ goods.id }}<span style="font-weight: bold">{{ name }}</span>
            </a-space>
          </a-row>
        </template>
        <a-space slot="action" slot-scope="text, goods">
          <a-button @click="selectGoods(goods)" size="small"> 选择 </a-button>
        </a-space>
      </selfTable>
    </a-card>
    <a-row justify="end" type="flex" style="margin-top: 15px">
      <a-space>
        <a-button @click="onCancel"> 取消 </a-button>
        <a-button type="primary" @click="onOk"> 确认 </a-button>
      </a-space>
    </a-row>
  </a-modal>
</template>

<script>
import { list, storeList } from '@/api/diy/goods/goods'
import { selfTable } from '../Table/index.js'
import { list as catListApi } from '@/api/diy/goods/cat'

const columns = [
  {
    title: '商品信息',
    dataIndex: 'name',
    scopedSlots: { customRender: 'name' },
  },
  {
    title: '价格',
    dataIndex: 'price',
    width: '150px',
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '40px',
    scopedSlots: { customRender: 'action' },
  },
]

export default {
  name: 'SearchGoods',
  components: {
    selfTable,
  },
  data() {
    return {
      columns: columns,
      catList: [],
      showModal: true,
      selectGoodsList: [],
      loadData: (parameter) => {
        var newObj = Object.assign({}, parameter, this.queryParam)
        let params = {
          data: null,
        }
        newObj.mch_id = this.mchId
        params.data = newObj
        return list(newObj).then((res) => {
          return res.data
        })
      },
      queryParam: {
        name: '',
        store_id: '',
        status: 1,
      },
      pageNum: 0,
      pageNo: 1,
      loadStore: true,
      storeList: [],
    }
  },
  props: {
    zIndex: {
      type: Number,
      default: 1000,
    },
    countNum: {
      type: Number,
      default: 0,
    },
    value: {
      type: Boolean,
      default: true,
    },
    catId: {
      type: Number,
      default: 0,
    },
    mchId: {
      type: Number,
      default: 0,
    },
  },
  watch: {
    catId: {
      deep: true,
      handler(newVal, oldVal) {
        if (newVal > 0) {
          this.queryParam.cat_id = newVal
        }
      },
    },
    mchId: {
      deep: true,
      handler(newVal, oldVal) {
        if (newVal > 0) {
          console.log('aaaaaaaaaaaaaaaaa')
          this.queryParam.mch_id = newVal
        }
      },
    },
  },
  created() {
    // this.getCatList()
    // this.getGoodsList();
  },
  methods: {
    getGoodsList() {
      var newObj = {}
      Object.assign(newObj, null, this.queryParam)
      newObj.mch_id = this.mchId
      list(newObj).then((res) => {
        console.log('商品弹框', res)
        this.pageNum = res.data.totalCount
        return res.data
      })
    },
    refresh(row) {
      this.$nextTick(() => {
        this.$refs.table.refresh(true)
      })
    },
    onEdit(id) {
      if (id > 0) {
        this.$router.push({ name: 'GoodsEdit', query: { id: id } })
      } else {
        this.$router.push({ name: 'GoodsEdit' })
      }
    },
    /**
     * 获取分类
     */
    getCatList() {
      catListApi({
        url: '/goods/cat-list',
        is_show: -1,
        mch_id: this.mchId,
      }).then((res) => {
        this.catList = res.data.list
      })
    },
    selectGoods(goods) {
      if (this.countNum > 0 && this.selectGoodsList.length >= this.countNum) {
        return false
      }
      var goodsInfo = {
        id: goods.id,
        name: goods.name,
        cover_pic: goods.cover_pic,
        original_price: goods.original_price,
        price: goods.price,
        is_negotiable: goods.is_negotiable,
        storeInfo:goods.storeInfo ? goods.storeInfo : null
      }
      for (var i = 0; i < this.selectGoodsList.length; i++) {
        if (this.selectGoodsList[i].id === goodsInfo.id) {
          return false
        }
      }
      this.selectGoodsList.push(goodsInfo)
    },
    updateParam(value, type) {
      this.queryParam[type] = value
      this.refresh()
    },
    delSelect(index) {
      this.selectGoodsList.splice(index, 1)
    },
    onCancel() {
      this.$emit('input', false)
    },
    onOk() {
      this.$emit('input', false)
      this.$emit('onOk', this.selectGoodsList)
    },
    async handleSearch(e) {
      if(!e) {
        this.storeList = []
        this.queryParam.store_id = ''
        this.loadStore = true
        return
      }
      if (this.loadStore) {
        this.loadStore = false
        const res = await storeList({ name: e })
        console.log(res)
        this.loadStore = true
        if (res.code == 0) {
          this.storeList = res.data.data
        }
      }
    },
  },
}
</script>
<style lang="scss" scoped>
.permission-form {
  ::v-deep .permission-group {
    margin-top: 0;
    margin-bottom: 0;
  }
}

.modal-card-body {
  max-height: 60vh;
  overflow: scroll;
}

.search-item {
  margin-left: 10px;
}
.selectImg:hover {
  opacity: 0.4;
}
.img-list {
  margin-bottom: 10px;
  background-color: #f6ffed;
  border: 1px solid #b7eb8f;
  padding: 6px;
  position: sticky;
  top: -20px;
  z-index: 999999999;
}
</style>
